<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :is-show="true" @add-depts="addDeptsNodeData" />

        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" :props="defaultProps" default-expand-all>
          <!-- 用了一个行列布局 -->
          <template v-slot="{ data }">
            <tree-tools
              :node-data="data"
              @del-depts="reqGetDepartments"
              @add-depts="addDeptsNodeData"
              @edit-depts="editModification"
            />
          </template>
        </el-tree>
      </el-card>

      <!-- sync的使用步骤:
          1.在属性上添加sync修饰符
          2.在子组件内部可以修改值，有特殊的语法 this.$emit('update:自定义属性名',新值) -->
      <AddDepts
        ref="addDepts"
        :show-dialog.sync="showDialog"
        :depts-list="deptsList"
        :node-data="nodeData"
        @add-success="reqGetDepartments"
      />
    </div>
  </div>
</template>

<script>
import { reqGetDepartDetail, reqDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import treeTools from './components/tree-tools.vue'
import AddDepts from './components/add-depts.vue'
export default {
  name: 'Departments',
  components: { treeTools, AddDepts },
  data() {
    return {
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {}, // 标题
      showDialog: false, // 弹窗

      deptsList: [], // 用来储存最初的数据

      nodeData: {}, // 用来储存点击时候的数据

      loading: false // 页面加载效果
    }
  },
  created() {
    this.reqGetDepartments()
  },
  // 获取数据
  methods: {
    async reqGetDepartments() {
      this.loading = true // 开始加载效果
      const { data: res } = await reqDepartments()
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.deptsList = res.depts // 保留原始数据
      this.list = tranListToTreeData(res.depts, '')
      this.loading = false // 关闭加载效果
    },
    // 添加
    addDeptsNodeData(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    // 编辑修改
    async editModification(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      const res = await reqGetDepartDetail(this.nodeData.id)
      this.$refs.addDepts.form = res.data
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
